package posts

import (
	"center-data-platform/app/content/api/internal/svc"
	"center-data-platform/app/content/api/internal/types"
	"center-data-platform/app/content/rpc/content"
	"context"
	"encoding/json"
	"fmt"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreatePostLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCreatePostLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreatePostLogic {
	return &CreatePostLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CreatePostLogic) CreatePost(req *types.CreatePostReq) (resp *types.CreatePostResp, err error) {
	userId, _ := l.ctx.Value("user_id").(json.Number).Int64()
	add, err := l.svcCtx.ContentRpc.Add(l.ctx, &content.CreatePostReq{
		Type:        int64(req.Type),
		Title:       req.Title,
		Description: req.Description,
		Tags:        req.Tags,
		Content:     req.Content,
		Cover:       req.Cover,
		UserId:      userId,
	})
	if err != nil {
		return nil, err
	}
	fmt.Println("res", add)
	return &types.CreatePostResp{Id: add.Data.Id}, nil
}
